using Newtonsoft.Json;

namespace GeYiHome.PublicLibrary.Extension
{
    public static class JsonExtension
    {
        #region Json转换

        /// <summary>
        /// 将对象转换为Json字符串
        /// </summary>
        /// <param name="jsonData">The json data.</param>
        /// <returns>System.String.</returns>
        public static string AsJsonString(this object jsonData)
        {
            return JsonConvert.SerializeObject(jsonData, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
        }

        /// <summary>
        /// 将Json字符串转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr">The json string.</param>
        /// <returns>T.</returns>
        public static T AsJson<T>(this string jsonStr)
        {
            return JsonConvert.DeserializeObject<T>(jsonStr);
        }

        #endregion
    }
}
